package com.qztech.sdk.model;

import androidx.lifecycle.ViewModel;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.RxLifecycle;

import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;

public abstract class ArchViewModel extends ViewModel implements LifecycleProvider<ArchViewModel.ArchEvent> {

    protected enum ArchEvent {
        CLEARED
    }

    private final BehaviorSubject<ArchEvent> archLifecycleSubject = BehaviorSubject.create();

    @NonNull
    @Override
    public final Observable<ArchEvent> lifecycle() {
        return archLifecycleSubject;
    }

    @NonNull
    @Override
    public final <T> LifecycleTransformer<T> bindUntilEvent(@NonNull ArchEvent event) {
        throw new UnsupportedOperationException("use bindToLifecycle");
    }

    @Override
    public final <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycle.bind(archLifecycleSubject);
    }

    @CallSuper
    @Override
    protected void onCleared() {
        archLifecycleSubject.onNext(ArchEvent.CLEARED);
        super.onCleared();
    }

}
